package com.pengke.pangu.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pengke.pangu.annotation.SLog;
import com.pengke.pangu.common.enums.BizType;
import com.pengke.pangu.common.utils.R;
import com.pengke.pangu.service.sys.entity.SysUser;
import com.pengke.pangu.service.sys.service.ISysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    @GetMapping("/getSysUserById")
    @SaCheckPermission("sys:user:read")
    public R getSysUserById(Integer id){
        log.info("getSysUserById");
        return R.ok(sysUserService.getById(id));
    }

    @GetMapping("/getSysUserList")
    @SaCheckPermission("sys:user:read")
    @SLog(model="用户管理", bizType = BizType.QUERY)
    public R getSysUserList(String keyword, Page<SysUser> page){
        log.info("getSysUserAllList");
        Map<String,Object> map = new HashMap<>();
        List<SysUser> list = sysUserService.list(page,Wrappers.<SysUser>query().lambda()
                .like(SysUser::getAccount,keyword).or()
                .like(SysUser::getUserName,keyword).or()
                .like(SysUser::getPhone,keyword)
                .orderByAsc(SysUser::getOrderId)
        );
        Long total = sysUserService.count(Wrappers.<SysUser>query().lambda()
                .like(SysUser::getAccount,keyword).or()
                .like(SysUser::getUserName,keyword).or()
                .like(SysUser::getPhone,keyword).orderByAsc(SysUser::getOrderId));

        map.put("list",list);
        map.put("total",total);
        return R.ok(map);
    }

    @PostMapping("/createSysUser")
    @SaCheckPermission("sys:user:create")
    @SLog(model="用户管理", bizType = BizType.INSERT)
    public R createSysUser(@RequestBody SysUser sysUser){
        Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
        String initPwd = BCrypt.hashpw("123456", BCrypt.gensalt());
        sysUser.setCreateUser(userId);
        sysUser.setPassword(initPwd);
        sysUserService.save(sysUser);
        return R.ok();
    }

    @DeleteMapping("/deleteSysUserByIds")
    @SaCheckPermission("sys:user:delete")
    @SLog(model="用户管理", bizType = BizType.DELETE)
    public R deleteSysUserById(String ids){
        log.info("deleteSysUserById()");
        sysUserService.removeBatchByIds(Arrays.asList(ids.split(",")));
        return R.ok();
    }

    @PostMapping("/updateSysUserById")
    @SaCheckPermission("sys:user:update")
    @SLog(model="用户管理", bizType = BizType.UPDATE)
    public R updateSysUserById(@RequestBody SysUser sysUser){
        Integer userId = Integer.parseInt(StpUtil.getLoginId().toString());
        sysUser.setUpdateUser(userId);
        //入库和更新时间用数据库时间戳自动处理
        sysUser.setCreateTime(null);
        sysUser.setUpdateTime(null);
        sysUserService.updateById(sysUser);
        return R.ok();
    }

    @GetMapping("/exportUser")
    @SaCheckPermission("sys:user:export")
    @SLog(model="用户管理", bizType = BizType.OTHER)
    public R exportUser(){
        log.info("exportUser()");
//        sysUserService.updateById(sysUser);
        return R.ok();
    }

}
